package com.tozed.test;

import com.tozed.widget.TitleBar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TouchscreenTestActivity extends Base implements OnClickListener
{
	private static final String TAG = "TouchscreenTestActivity";

	private TitleBar mTitleBar;
	private Button mBtnPass, mBtnFail;
	private TextView mTextInfo = null;
	
	private boolean mIsFirstCreate = true;
	
	private static final int REQUEST_CODE_TOUCH_TEST = 1;
	private int mStage = TOUCH_TEST_STAGE_1_HORIZONTAL_LINE;
	
	 /**
     * Touchpanel test in 3 stage
     * stage 1: horizontal line test
     * stage 2: vertical line test
     * stage 3: multiple circle test
    */
    private static final int TOUCH_TEST_STAGE_1_HORIZONTAL_LINE = 0;
    private static final int TOUCH_TEST_STAGE_2_VERTICAL_LINE = 1;
    private static final int TOUCH_TEST_STAGE_3_MULTIPLE_CIRCLE = 2;
    
    private int mTestType = Constant.TYPE_TEST_SINGLE;

	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.v(TAG, "onCreate");
		setContentView(R.layout.test_touchpanel);
		
		//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		//this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		//		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		//setContentView(new TouchPadView(this));
		
		Intent intent = getIntent();
		if (intent != null)
			mTestType = intent.getIntExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
		
		mTitleBar = (TitleBar)findViewById(R.id.title_bar);
		mTitleBar.setTiteText(R.string.str_test_touchpanel);
		mTitleBar.hideLeftButton();
		mTitleBar.hideRightButton();
		
		mBtnPass = (Button)findViewById(R.id.btn_pass);
		mBtnFail = (Button)findViewById(R.id.btn_fail);
		mBtnPass.setOnClickListener(this);
		mBtnFail.setOnClickListener(this);
		mBtnPass.setVisibility(View.INVISIBLE);
		
		mTextInfo = (TextView)findViewById(R.id.text_tip);


        StringBuilder builder = new StringBuilder();
        builder.append(getString(R.string.str_title_touch)).append(": ")
        	.append(getString(R.string.str_touch_first_stage));
        mTextInfo.setText(builder.toString());
        
        if (mIsFirstCreate){
        	mIsFirstCreate = false;
        	startTouchTestHorizontalActivty();
        }
	}
	
	private void startTouchTestHorizontalActivty()
	{
		Intent intent = new Intent();
		intent.setClass(this, TouchTestHorizontal.class);
        startActivityForResult(intent, REQUEST_CODE_TOUCH_TEST);
	}
	
	@Override
    protected void onResume() {
		Log.v(TAG, "onResume");
        super.onResume();
    }
	
	@Override
    protected void onPause() {
		Log.v(TAG, "onPause");
        super.onPause();
    }
	
	@Override
    protected void onDestroy() {
		Log.v(TAG, "onDestroy");
        super.onDestroy(); 
    }
	
	  @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) 
	  {
        super.onActivityResult(requestCode, resultCode, data);
        Log.v(TAG, "onActivityResult requestCode = " + requestCode + " resultCode = " + resultCode);
        
        if (requestCode == REQUEST_CODE_TOUCH_TEST) {
            if (resultCode == Activity.RESULT_OK) {
                if (mStage == TOUCH_TEST_STAGE_1_HORIZONTAL_LINE) {
                    StringBuilder builder = new StringBuilder();
                    builder.append(getString(R.string.str_title_touch)).append(": ")
                    .append(getString(R.string.str_touch_second_stage));
                    mTextInfo.setText(builder.toString());
                    
                    Intent intent = new Intent();
                    intent.setClass(this, TouchTestVertical.class);
                    startActivityForResult(intent, REQUEST_CODE_TOUCH_TEST);
                    mStage = TOUCH_TEST_STAGE_2_VERTICAL_LINE;
                } else if (mStage == TOUCH_TEST_STAGE_2_VERTICAL_LINE) {
                    StringBuilder builder = new StringBuilder();
                    builder.append(getString(R.string.str_title_touch)).append(": ")
                    	.append(getString(R.string.str_touch_third_stage));
                    mTextInfo.setText(builder.toString());
                    
                    Intent intent = new Intent();
                    intent.setClass(this, TouchTestMultiple.class);
                    startActivityForResult(intent, REQUEST_CODE_TOUCH_TEST);
                    mStage = TOUCH_TEST_STAGE_3_MULTIPLE_CIRCLE;
                } else if (mStage == TOUCH_TEST_STAGE_3_MULTIPLE_CIRCLE) {
                    StringBuilder builder = new StringBuilder();
                    builder.append(getString(R.string.str_title_touch)).append(": ")
                    	.append(getString(R.string.str_test_pass));
                    mTextInfo.setText(builder.toString());
                    
                    mBtnPass.setVisibility(View.VISIBLE);
                    if (mTestType == Constant.TYPE_TEST_AGING) {
                    	mBtnPass.performClick();
                    }
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                StringBuilder builder = new StringBuilder();
                builder.append(getString(R.string.str_title_touch)).append(": ")
                	.append(getString(R.string.str_test_fail));
                mTextInfo.setText(builder.toString());
            } else {
                Log.v(TAG, "onActivityResult resultCode invalid");
            }
        } else {
            Log.v(TAG, "onActivityResult data null");
        }
    }
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_pass:
			Intent intent = new Intent();
			intent.putExtra("KEY_TEST_STATUS", 1);
			setResult(RESULT_OK, intent);
			finish();
			break;
			
		case R.id.btn_fail:
			Intent intent2 = new Intent();
			intent2.putExtra("KEY_TEST_STATUS", 0);
			setResult(RESULT_OK, intent2);
			finish();
			break;
			
		default:
			break;
		}
	}
	
	@Override
    public void onBackPressed() {
        if (mTestType == Constant.TYPE_TEST_AGING) {
        	Intent intent = new Intent();
			intent.putExtra("KEY_TEST_STATUS", 0);
			setResult(RESULT_OK, intent);
        }
        super.onBackPressed();
    }
}
